package com.example.test.controller;

import com.example.test.bean.DTO.ChangePasswordRequest;
import com.example.test.bean.DTO.LoginRequest;
import com.example.test.bean.DTO.RegisterRequest;
import com.example.test.bean.domain.ShopUser;
import com.example.test.common.ResponseHelper;
import com.example.test.service.ShopUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Tag(name = "用户管理")
public class ShopUserController {

	@Autowired
	private ShopUserService shopUserService;

	@Operation(summary = "注册用户")
	@PostMapping("/register")
	public ResponseHelper<?> register(@RequestBody RegisterRequest request) {
		ShopUser user = shopUserService.register(request.getMobile(), request.getPassword(), request.getNickname());
		return ResponseHelper.genResponse();
	}

	@Operation(summary = "用户登录")
	@PostMapping("/login")
	public ResponseHelper<?> login(@RequestBody LoginRequest request) {
		Map<String, Object> response = shopUserService.login(request.getMobile(), request.getPassword());
		return ResponseHelper.genResponse(response);
	}

	@Operation(summary = "用户修改密码")
	@PostMapping("/changePassword")
	public ResponseEntity<?> changePassword(@RequestBody ChangePasswordRequest request) {
		shopUserService.changePassword(request.getUserId(), request.getOldPassword(), request.getNewPassword());
		return ResponseEntity.ok(Collections.singletonMap("message", "密码修改成功"));
	}
}
